package com.zjh.photoshopstudy.app.download;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okserver.download.DownloadTask;
import com.lzy.okserver.task.XExecutor;
import com.zjh.photoshopstudy.R;
import com.zjh.photoshopstudy.app.history.onItemCheck;

import java.util.ArrayList;
import java.util.List;

public class DownloadActivity extends AppCompatActivity implements onItemCheck<DownloadTask>, XExecutor.OnAllTaskEndListener {
    DownloadAdapter adapter;
    TextView selectBtn;
    TextView tip;
    LinearLayout layout;
    private List<DownloadTask> deleteList = new ArrayList<>();
    int type;
    private boolean isAllSelect = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);
        Toolbar toolbar = findViewById(R.id.toolbar4);
        layout = findViewById(R.id.layout);
        tip = findViewById(R.id.no_download_tip);
        selectBtn = findViewById(R.id.select_btn2);

        RecyclerView recyclerView = findViewById(R.id.downloadList);
        adapter = new DownloadAdapter(this, this);
        adapter.addOnAllTaskEndListener(this);
        if (getIntent().getStringExtra("type").equals("video")) {
            type = DownloadingAdapter.TYPE_VIDEO;
            toolbar.setTitle("已下载视频");
        } else {
            type = DownloadingAdapter.TYPE_MATERIA;
            toolbar.setTitle("已下载素材");
        }
        adapter.updateData(type);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        if (adapter.getItemCount() == 0) {
            tip.setVisibility(View.VISIBLE);
            selectBtn.setVisibility(View.INVISIBLE);
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                doCancel();
                break;
            case R.id.delete:
                doDelete();
                break;
            case R.id.select_btn2:
                clickSelectBtn();
                break;
        }
    }

    private void doCancel() {
        layout.setVisibility(View.INVISIBLE);
        adapter.setSelect(false);
        adapter.setIsSelectAll(false);
        deleteList.clear();
        adapter.updateData(type);
        selectBtn.setText("管理");
    }

    private void doDelete() {
        //mPresent.deleteCollect(collectionList);
        for (DownloadTask task : deleteList){
            task.remove(true);
        }
        doCancel();
    }


    private void clickSelectBtn() {
        if (adapter.isSelect()) {
            deleteList.clear();
            if (isAllSelect) {
                adapter.setIsSelectAll(false);
            } else {
                adapter.setIsSelectAll(true);
            }
            adapter.updateData(type);
        } else {
            adapter.setSelect(true);
            adapter.updateData(type);
            layout.setVisibility(View.VISIBLE);
            selectBtn.setText("全选");
            adapter.setSelect(true);
        }
    }

    @Override
    public void onItemCheck(DownloadTask entity) {
        deleteList.add(entity);
        updateBtn();
    }

    private void updateBtn() {
        if (deleteList.size() == adapter.getItemCount()) {
            isAllSelect = true;
            selectBtn.setText("全不选");
        } else {
            isAllSelect = false;
            if (adapter.isSelect()) {
                selectBtn.setText("全选");
            } else {
                selectBtn.setText("管理");
            }
        }
    }

    @Override
    public void onItemCancelCheck(DownloadTask entity) {
        deleteList.remove(entity);
        updateBtn();
    }

    @Override
    public void onAllTaskEnd() {
        if (adapter.getItemCount() != 0) {
            tip.setVisibility(View.INVISIBLE);
            selectBtn.setVisibility(View.VISIBLE);
        } else {
            tip.setVisibility(View.VISIBLE);
            selectBtn.setVisibility(View.INVISIBLE);
        }
    }
}
